/**
 * Created by Administrator on 2017/8/30.
 */
$(document).ready(function () {

    var spuId=locationSearcher('spuId');

    var skuId=locationSearcher('skuId');



    //商品信息
    var infoTpl=$('#info-template').html();
    var infoCmp=Handlebars.compile(infoTpl);
    //收货地址
        var addressTpl=$('#address-template').html();
        var addressCmp=Handlebars.compile(addressTpl);

    (function () {
        $.ajax({
            url:C.interface.getShouHuoAddress,
            type:'get',
            dataType:'json',
            data:{
                token:C.token
            },
            success:function (response) {
                if (response.result=='success'){
                    var data=response.data;
                    //console.log(data);
                    var addressData=addressCmp(data);
                    $('#default-address').html(addressData);



                    var noAddress=data.isHaveAddress;
                    if (noAddress!='0'){

                        //修改收货地址
                        $('.order-address').click(function () {
                            location.href='change_address.html';
                        });

                    }else {
                        $('#set-address').click(function () {
                            location.href='seckill_add_address.html';
                        })
                    }

                }else if (response.result === 'login'){
                    alert('登录过期请重新登录');
                    againLogin();
                }else {
                    alert(response.errorMsg);
                }
            }
        });
    })();

    (function () {
        var buyId = localStorage.getItem('orderSubmitId');
        $.ajax({
            url:C.interface.preCreateOrder,
            type:'get',
            dataType:'json',
            data:{
                token:C.token,
                products:buyId

            },
            success:function (response) {
                if (response.result=='success'){

                    var data=response.data;

                    /*var skus = data.skus;
                    for (var j = 0; j < skus.length; j++) {
                        skus[j].skuDetail = JSON.parse(skus[j].skuDetail);
                    }
                    console.log(data);

                    //限购数量
                    buyLimitPerUser=data.skus[0].buyLimitPerUser;
                    //console.log(buyLimitPerUser);
*/
                    $('#confirm-shop-list').html(infoCmp(data));
                    //$('.xiangou-number').html(buyLimitPerUser);

                    updateAllSales();

                    //调用加减  分别给+ -绑定点击事件
                    $('.count-plus').bind('click',cartJia);
                    $('.count-jian').bind('click',cartJian);

                    var setAddress=$('#set-address').length;

                    if (setAddress > 0){
                        $('#go-submit').unbind().bind('click',function () {
                            alert('请填写收货地址');
                        });
                    }else {
                        //提交订单
                        $('#go-submit').unbind().bind('click',submitOrder);
                    }



                }
            }
        });
    })();


    /*商品数量加*/
    function cartJia() {
        //清除默认行为 (例如：a标签,点击会跳转)
        event.preventDefault();

        // var id = parseInt($(this).parents('.navList').find('.goodImgUrl').attr('href').split('=')[1]);

        //获取数量
        var number=parseInt($(this).siblings('.count-number').html());

        //三元运算符:如果商品数量=0的时候就让数量=1
        number=number>0?number : 1;
        //每次都累加1
        number++;
        //单个商品超过99个就不让其再添加下去
        if (number>99){
            //$('.count-plus').addClass('none-click-button').removeClass('count-plus');
            return false;
        }

        //把累加完的数放入到中间数量显示区域
        $(this).siblings('.count-number').html(number);
        //调用价格函数
        updateAllSales(number);
    }


    /*商品数量减*/
    function cartJian() {
        event.preventDefault();
        var number=parseInt($(this).siblings('.count-number').html());
        number = number > 0 ? number : 1;
        number--;
        updateAllSales(number);
        if (number==0){
            return false;
        }else {
            $(this).siblings('.count-number').html(number);
            updateAllSales(number);
        }
    }

    function updateAllSales(number){
        var num=0,
            totalPrice=0;
        num+=parseInt($('.count-number').html());
        var thisPrice=$('.shop-danjia').html();
        var payCount=$('.count-number').html();
        totalPrice = addFloatNum(totalPrice, accMul(parseFloat(thisPrice) , parseInt(payCount)), 'jia');
        $('.count-number').html(num);
        $('.pay-count-sales').html(toDecimal2(totalPrice));
    }

    function submitOrder() {

        var number=$('.count-number').html();
        var note=$('#taContent').html();
        var goPayId=localStorage.getItem('orderSubmitId');
        var addressId = $('.order-address-name').data('addressid');
        $.ajax({
            url:C.interface.createOrder,
            type:'get',
            dataType:'json',
            data:{
                products:goPayId,
                amount:number,
                token:C.token,
                note:note,
                addressId:addressId
            },
            success:function (response) {
                if (response.result == 'success'){

                    localStorage.setItem('orderId',response.data.orderId);
                    localStorage.setItem('totalMoney',response.data.totalMoney);

                    location.href='confirm-pay.html?version='+C.version;
                }
            }
        })




    }

    



});